<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\index\controller;

use think\Controller;
use think\Db;
use app\index\model\UsersModel;
class Base extends Controller{
    public function _initialize(){
        if(is_null(session('user_id')) && (is_null(input('param.user_id'))  || input('param.user_id')<=0)){
            // session('user_id',1);

            $loginUrl = url('login/login');
            $this->redirect($loginUrl);
        }
        // var_dump($_SESSION);
        // DIE;
        if(is_null(session('user_id'))) session('user_id',input('param.user_id'));
        $model = new UsersModel();
        $hasUser = $model->findUserById(session('user_id'));
        if(empty($hasUser)){
            $loginUrl = url('login/login');
            $this->redirect($loginUrl);
        }
        if(!is_null(input('param.recode'))){
            $uid=base64_decode(input('param.recode'));
            if(is_numeric($uid) && $uid>0){
                if($hasUser['first_leader']==0){
                    $first_leader=$model->findUserById($uid);
                    if(!empty($first_leader)){
                        $map=array('first_leader'=>$first_leader['user_id'],'second_leader'=>$first_leader['first_leader'],'third_leader'=>$first_leader['second_leader']);
                        $model->updateUser($map,$hasUser['user_id']);
                    }
                }
            }
        }
    }
}